<?php

namespace app\api\controller;
error_reporting(0);

use app\admin\service\ProofService;
use app\api\service\GPService;
use app\api\service\HaiDing;
use app\api\service\JinDie;
use app\api\service\Request;
use app\api\service\SaleService;
use app\common\model\tang\MerchantList;
use app\common\model\tang\MerchantReadingLog;
use app\common\model\tang\MonthWaterLog;
use app\common\model\tang\SaleReportLog;
use think\facade\Cache;
use think\cache\driver\Redis;

//use app\api\service\MyAES;
use app\api\service\MarketingService;
use app\api\service\MemberService;
use app\api\service\message;
use app\api\service\MyAES;
use app\common\energyApi\Energy;
use app\common\model\tang\BalanceLog;
use app\common\model\tang\Config;
use app\common\model\tang\Cost;
use app\common\model\tang\CostType;
use app\common\model\tang\Invoice;
use app\common\model\tang\KwhLog;
use app\common\model\tang\KwhWaterLog;
use app\common\model\tang\Merchant;
use app\common\model\tang\MerchantDayMoney;
use app\common\model\tang\MerchantInvoice;
use app\common\model\tang\MerchantMoney;
use app\common\model\tang\MerchantMonthMoney;
use app\common\model\tang\MonthElectricityDegree;
use app\common\model\tang\MonthSoleElectricityLog;
use app\common\model\tang\WatchLog;
use app\common\model\tang\WatchMaintain;
use Imagick;

class Test extends Base
{
    /**
     * 线上充值传入金蝶
     */
    public function exportOnline()
    {
        var_dump(1111);
    }
    /**
     *电费账单传入金蝶
     */
    public function test()
    {


        $start_time = strtotime(date('Y-m-d ' . '06:00:00'));
        $end_time = strtotime(date('Y-m-d ' . '07:40:00'));
        $YesterDay = date("Y-m-d", strtotime("-1 day"));
        $month = date("Y-m", strtotime("-1 month"));
        $monthsss = date("Y-m", strtotime("-2 month"));
        $nowMonth = date("Y-m");
        $jin = new JinDie();
        $config = new Config();
        $message = new message();
        $merchantDayModel = new MerchantDayMoney();
        $MerchantMonthMoney = new MerchantMonthMoney();
        $Allmerchant = (new WatchMaintain())->with(['merchant'])->where(['type' => 1, 'status' => 1])->where('merchant_id', 120)->select()->toArray();
        $configInfo = $config->find(1)->toArray();
        $money = (new MerchantMoney())->where('dateTime', $month)->find();
        if (!empty($Allmerchant)) {
            foreach ($Allmerchant as $v) {
                $data_day = [];
                $EleInfo = [];
                $frozen_balance = 0;
                $data = [];
                $Electricity = [];
                $res = [];
                $first = [];
                $dataInfo = [];
                $EleInfo = [];
                $dayInfo = [];
                $oneDayMoney = 0; //上月的消费   oOGTi4lmrNHNdUY4aP-nX3am7O2k
                if ($v['is_sole'] == 1) { //单一用电的
                    $dandata = [
                        "water_or_electricity" => 2,
                        'type' => 3,
                        'day_month' => 2,
                        'pointId' => $v['point'],
                        'startdate' => $YesterDay,
                        'enddate' => ''
                    ];
                    $EleInfo = $jin->getElectricity($dandata);
                    if (empty($EleInfo)) {
                        $EleInfo = $jin->getElectricity($dandata);
                    }
                    $danlog = (new KwhWaterLog())->where(['datelog' => $YesterDay, 'pointId' => $v['pointId']])->find();
                    if (empty($danlog) && !empty($EleInfo)) {
                        $dandata = [
                            'merchant_id' => $v['merchant_id'],
                            'pointId' => $v['pointId'],
                            'dataTime' => $nowMonth,
                            'end' => $EleInfo['endbm'],
                            'start' => $EleInfo['startbm'],
                            'create_time' => time(),
                            'use_type' => $v['use_type'],
                            'datelog' => $YesterDay
                        ];
                        (new KwhWaterLog())->save($dandata);
                        if ($v['merchant_id'] == 68 || $v['merchant_id'] == 120) {//指定用户催费
                            if (!empty($money)) {
                                $danprice = (new MonthSoleElectricityLog())->order('dataTime desc')->where(['merchant_id' => $v['merchant_id'], 'status' => 1])->value('price');
                                if (!empty($danprice)) {
                                    $money['linmoney'] = $danprice;
                                }
                                $frozen_balance = bcmul($EleInfo['energyp'], $money['linmoney'], 2);
                                dump($frozen_balance);
                                if ($v['balance'] > 0 && !empty($v['merchant_id'])) {
                                    $balance = bcsub($v['balance'], $frozen_balance * 100);
                                    (new Merchant())->where('id', $v['merchant_id'])->update(['balance' => $balance]);
                                    (new Merchant())->where('id', $v['merchant_id'])->inc('frozen_balance', bcadd($v['frozen_balance'], $frozen_balance * 100))->update();
//                                    //查今天是否有记录
                                    $dayInfo = $merchantDayModel->where(['merchant_id' => $v['merchant_id'], 'day' => $YesterDay])->find();
                                    $monthInfo = $MerchantMonthMoney->where(['merchant_id' => $v['merchant_id'], 'dataTime' => $nowMonth])->find();
                                    if (empty($dayInfo)) {
                                        $merchantDay['merchant_id'] = $v['merchant_id'];
                                        $merchantDay['code'] = $v['code'];
                                        $merchantDay['balance'] = $v['balance'];
                                        $merchantDay['use_yue'] = $balance;
                                        $merchantDay['name'] = $v['name'];
                                        $merchantDay['company'] = $v['company'];
                                        $merchantDay['money'] = $frozen_balance * 100;
                                        $merchantDay['dataTime'] = $nowMonth;
                                        $merchantDay['day'] = $YesterDay;
                                        $merchantDay['create_time'] = time();
                                        $merchantDayModel->insert($merchantDay);
                                        if (empty($monthInfo)) {
                                            unset($merchantDay['day']);
                                            $MerchantMonthMoney->insert($merchantDay);
                                        } else {
                                            $MerchantMonthMoney->update([
                                                'money' => bcadd($monthInfo['money'], bcmul($frozen_balance, 100)),
                                                'balance' => bcadd($v['balance'], $v['frozen_balance']),
                                                'use_yue' => $balance,
                                            ], ['id' => $monthInfo['id']]);
                                        }
                                    } else {
                                        $yingMoney = bcadd($dayInfo['money'], bcmul($frozen_balance, 100));
                                        $merchantDayModel->update([
                                            'money' => $yingMoney,
                                            'balance' => bcadd($v['balance'], $v['frozen_balance']),
                                            'use_yue' => $balance
                                        ], ['id' => $dayInfo['id']]);
                                        $MerchantMonthMoney->update([
                                            'money' => bcadd($monthInfo['money'], bcmul($frozen_balance, 100)),
                                            'balance' => bcadd($v['balance'], $v['frozen_balance']),
                                            'use_yue' => $balance,
                                        ], ['id' => $monthInfo['id']]);
                                    }
                                }
                            }
                        }

                    }

                }
            }
        }
    }

    public function payCost()
    {
        $balance = (new BalanceLog())->where('status',1)->select();
        foreach ($balance as $k=>$v){
            if(empty($v['order_sn'])||empty($v['transaction_id'])||empty($v['bill_no'])){
                $order_sn = OrderSn();
                if(!empty($v['charge_time'])){
                    $bill_no = 'CZMX'.date('Ymd', $v['charge_time']).rand(10000,99999);
                }else{
                    $bill_no = 'CZMX'.date('Ymd', strtotime($v['create_time'])).rand(10000,99999);
                }
                (new BalanceLog())->update(['order_sn'=>$order_sn,'transaction_id'=>$order_sn,'bill_no'=>$bill_no],['id'=>$v['id']]);
            }
        }
        dd($balance);
        $jin = new JinDie();
//        //单独电费读取
        $data_day = [
            'water_or_electricity' => 2,
            'type' => 2,
            'day_month' => 3,
            'pointId' => '52737',
            'startdate' => '2023-11',
            'enddate' => ''
        ];
//////
////        $month = date("Y-m", strtotime("-1 month"));
////        $monthsss = date("Y-m", strtotime("-2 month"));
        $res = $jin->getElectricity($data_day);
        dd($res);
//        $first = (new MonthElectricityDegree())->where(['pointId' => '000000281968', 'dataTime' => $month])->find();
//        $lastMonth = (new MonthElectricityDegree())->where(['pointId' => '000000281968', 'dataTime' => $monthsss])->find();
//        if (empty($first) && !empty($res)) {
//            if (($res['feng']['startbm'] == 'null' && $res['feng']['startbm'] != 0)||!$res['feng']['endbm']) {
//            } else {
//                $Electricity = [
//                    'merchant_id' => 111,
//                    'pointId' => '000000281968',
//                    'pointName' => $res['name'],
//                    'dataTime' => $month,
//                    'fengStart' => $res['feng']['startbm'],
//                    'fengEnd' => $res['feng']['endbm'],
//                    'fengPower' => $res['feng']['energyp'],
//                    'guStart' => $res['gu']['startbm'],
//                    'guEnd' => $res['gu']['endbm'],
//                    'guPower' => $res['gu']['energyp'],
//                    'pingStart' => $res['ping']['startbm'],
//                    'pingEnd' => $res['ping']['endbm'],
//                    'pingPower' => $res['ping']['energyp'],
//                    'rate' => $v['rate'],
//                    'use_type' => $v['use_type'],
//                    'create_time' => time(),
//                ];
//                if (!empty($lastMonth)) {
//                    $Electricity['guStart'] = $lastMonth['guEnd'];
//                    $Electricity['guPower'] = bcmul(bcsub($res['gu']['endbm'], $lastMonth['guEnd'], 2), $v['rate'], 2);
//                }
//                (new MonthElectricityDegree())->save($Electricity);
//            }
//
//        }
//
//        dd(111);
//        //单独水费读取
        $Allmerchant = (new WatchMaintain())->where('pointId','000090203461')->with(['merchant'])->where(['type'=>2,'status'=>1])->select()->toArray();
        $jin = new JinDie();
        $month = '2023-11';
        $item['pointId'] = '48182';
        $item['point'] = '48182';
        $item['merchant_id'] = '251';
        $monthdata = [
            "water_or_electricity" => 1,
            'type' => 3,
            'day_month' => 3,
            'pointId' => $item['point'],
            'startdate' => $month,
            'enddate' => ''
        ];

        $port_data_month = $jin->getElectricity($monthdata);
        foreach ($Allmerchant as $v) {

        }

        dd($port_data_month);
//        if (!empty($port_data_month)) {
//            if (!empty($port_data_month['endbm']) && !empty($port_data_month['startbm'])) {
//                if ($port_data_month['startbm'] > $port_data_month['endbm']) {
//                    $len = strlen($port_data_month['startbm']);
//                    if ($len == 4) {
//                        $end = 9999;
//                    } else {
//                        $end = 99999;
//                    }
//                    $water = [
//                        'start' => $port_data_month['startbm'],
//                        'end' => $end,
//                        'pointId' => $item['pointId'],
//                        'merchant_id' => $item['merchant_id'],
//                        'dataTime' => $month,
//                        'create_time' => time()
//                    ];
//                    $port_data_month['startbm'] = 0;
//                    (new MonthWaterLog())->save($water);
//                }
//                $water = [
//                    'start' => $port_data_month['startbm'],
//                    'end' => $port_data_month['endbm'],
//                    'pointId' => $item['pointId'],
//                    'merchant_id' => $item['merchant_id'],
//                    'dataTime' => $month,
//                    'create_time' => time()
//                ];
//                (new MonthWaterLog())->save($water);
//            }
//        }
//    }
    }
}